package com.benew.ntt.evaluate

import android.util.SparseArray
import com.benew.ntt.evaluate.constant.EvalConst
import com.blankj.utilcode.util.FileIOUtils
import com.ntt.core.nlogger.NLogger
import org.json.JSONArray
import java.lang.Exception

object EvalScore {
    private val TAG = EvalScore.javaClass.simpleName

    //默认难度
    private var mDefLevel: Int? = null
    private val mLevelMap = SparseArray<JSONArray>()

    var STAR_ONE = 60.0
    var STAR_TWO = 70.0
    var STAR_THREE = 80.0
    var STAR_FOUR = 90.0
    var STAR_FIVE = 96.0

    /**
     * 设置评测难易度配置
     */
    fun setLevelConfig() {
        var json: String? = null
        val filePath = EvalConst.getScoreFilePath()
        try {
            json = FileIOUtils.readFile2String(
                filePath
            )

            if (!json.isNullOrBlank()) {
                NLogger.d(TAG, "评测难易度分数配置：json=$json", "filePath=$filePath")
                val levelArray = JSONArray(json)
                for (i in 0 until levelArray.length()) {
                    val levelObject = levelArray.optJSONObject(i)
                    val level = levelObject?.optInt("level")

                    val evalCfg = levelObject?.optJSONObject("evalCfg")
                    val scores = evalCfg?.optJSONArray("scores")
                    if (level != null && scores != null) {
                        mLevelMap.put(level, scores)

                        //默认难度
                        val defaultLevel = levelObject.optBoolean("defaultLevel")
                        if (defaultLevel) {
                            mDefLevel = level
                        }

                        //如果服务器没有配置默认的，那就默认“正常”
                        val name = levelObject.optString("name")
                        if ("正常" == name) {
                            if (mDefLevel == null) {
                                mDefLevel = level
                            }
                        }
                    }
                }
            } else {
                NLogger.d(TAG, "没拿到评测难易度分数配置：json=$json", "filePath=$filePath")
            }
        } catch (e: Exception) {
            NLogger.d(
                TAG, "拿到评测难易度分数配置失败：json=$json",
                "Exception=${e.message}", "filePath=$filePath"
            )
        }
    }

    /**
     * 设置评测难易度配置
     * {"level":number,"sorces":[一星分数，二星分数，三星分数，四星分数，五星分数]}
     */
    fun getScoreConfig(): Map<String, Any> {
        var json: String? = null
        val filePath = EvalConst.getLevelFilePath()
        var level = mDefLevel
        try {

            json = FileIOUtils.readFile2String(
                filePath
            )
            level = if (json.isNullOrBlank()) {
                if (mDefLevel != null) {//如果没有默认配置项
                    NLogger.d(
                        TAG, "拿到评测难易度配置失败,默认使用mDefLevel=${mDefLevel}难度",
                        "json=$json",
                        "filePath=$filePath"
                    )
                    mDefLevel!!
                } else {
                    val keyAt = mLevelMap.keyAt(0)
                    NLogger.d(
                        TAG, "拿到评测难易度配置失败,使用第一个难度，level=${keyAt}难度",
                        "json=$json",
                        "filePath=$filePath"
                    )
                    keyAt
                }
            } else {
                json.toInt()
            }
            val scores = mLevelMap.get(level)
            if (scores.length() > 0) {
                for (i in 0 until scores.length()) {
                    val item = scores.optJSONObject(i)
                    when (item.optInt("level")) {
                        1 -> {
                            STAR_ONE = item.optDouble("score")
                        }
                        2 -> {
                            STAR_TWO = item.optDouble("score")
                        }
                        3 -> {
                            STAR_THREE = item.optDouble("score")
                        }
                        4 -> {
                            STAR_FOUR = item.optDouble("score")
                        }
                        5 -> {
                            STAR_FIVE = item.optDouble("score")
                        }
                    }
                }
            }
            NLogger.d(
                TAG, "评测难易度配置：当前难度level=$level",
                "json=${json}",
                "STAR_ONE=${STAR_ONE}",
                "STAR_TWO=${STAR_TWO}",
                "STAR_THREE=${STAR_THREE}",
                "STAR_FOUR=${STAR_FOUR}",
                "STAR_FIVE=${STAR_FIVE}",
                "filePath=$filePath"
            )
        } catch (e: Exception) {
            NLogger.d(
                TAG, "拿到评测难易度配置失败：json=$json",
                "Exception=${e.message}", "filePath=$filePath"
            )
        }
        val config = hashMapOf<String, Any>()
        config["level"] = level ?: 0
        config["scores"] = doubleArrayOf(STAR_ONE, STAR_TWO, STAR_THREE, STAR_FOUR, STAR_FIVE)
        return config

    }

    /**
     * 设置评测难易度配置
     * [一星分数，二星分数，三星分数，四星分数，五星分数]
     */
    fun getScore() = getScoreConfig()["scores"] as DoubleArray

}